package com.rdxer.core.jpaex;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import lombok.SneakyThrows;

import javax.persistence.AttributeConverter;
import java.util.List;

public class ListListStringToJsonConverter implements AttributeConverter<List<List<Object>>,String> {

    ObjectMapper objectMapper = new ObjectMapper();
    TypeFactory typeFactory = objectMapper.getTypeFactory();

    @SneakyThrows
    @Override
    public String convertToDatabaseColumn(List<List<Object>> o) {
        return objectMapper.writeValueAsString(o);
    }

    @SneakyThrows
    @Override
    public List<List<Object>> convertToEntityAttribute(String s) {
        return objectMapper.readValue(s, typeFactory.constructCollectionType(List.class, List.class));
    }
}